#include <jni.h>
#include <stdio.h>
#include <string.h>
#include "com_xqq_lottery_NativeProtocal.h"
#include "com_xqq_lottery_protocal_Body.h"
#include <android/log.h>

#define LOGW(a)  __android_log_write(ANDROID_LOG_WARN,"FlowLayout",a)

const char *AGENTER_PASSWORD = "9ab62a694d8bf6ced1fab6acd48d02f8";
const char *SCERET_KEY = "9b2648fcdfbad80f";   //秘钥
const char *ENCODE = "ENCODE";           //解密
const char *DECODE = "DECODE";           //加密


JNIEXPORT jstring JNICALL Java_com_xqq_lottery_NativeProtocal_getAgenterPassword
  (JNIEnv *env, jclass obj)
{
	return env->NewStringUTF(AGENTER_PASSWORD);
}

JNIEXPORT jstring JNICALL Java_com_xqq_lottery_NativeProtocal_compressDigest
  (JNIEnv *env, jclass obj, jstring time, jstring body)
{
	char* c_time = jstringTostring(env,time);
	char* c_body = jstringTostring(env,body);
	LOGW(c_time);
	int len = strlen(AGENTER_PASSWORD)+strlen(c_time)+strlen(c_body);
	char *content = (char*)malloc(sizeof(char)*len+1);
	memset(content,0,len+1);
	strcat(content,c_time);
	strcat(content,AGENTER_PASSWORD);
    strcat(content,c_body);
    LOGW(content);
    jstring jcontent = env->NewStringUTF(content);

    releaseStr(&c_time);
    releaseStr(&c_body);
    releaseStr(&content);

	//char* ת���� jstring
	jclass clazz = env->FindClass("com/xqq/lottery/NativeProtocal");
    jmethodID constructMethodId = env->GetMethodID(clazz,"<init>","()V");
    jobject NativeProtocalObj = env->NewObject(clazz,constructMethodId);
	jmethodID methodId = env->GetMethodID(clazz,"md5Hex","(Ljava/lang/String;)Ljava/lang/String;");
	jstring compressJContent = (jstring)env->CallObjectMethod(NativeProtocalObj,methodId,jcontent);
	return compressJContent;
}


JNIEXPORT jstring JNICALL Java_com_xqq_lottery_NativeProtocal_compressBody
  (JNIEnv * env, jclass obj, jstring body)
{
	return body;
}



//DES加密
JNIEXPORT jstring JNICALL Java_com_xqq_lottery_protocal_Body_getBodyInsideDESInfo
  (JNIEnv *env, jobject clazz,jstring jbodyContent)
{
	jclass desClazz = env->FindClass("com/xqq/lottery/util/DES");
	jmethodID desInitMethodId = env->GetMethodID(desClazz,"<init>","()V");
 	jobject desObj = env->NewObject(desClazz,desInitMethodId);
	jmethodID authcodeId = env->GetMethodID(desClazz,"authcode","(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
    jstring jdecodestr = env->NewStringUTF("DECODE");
    jstring jsceretkey = env->NewStringUTF(SCERET_KEY);
    //调用Des的加密方法
    jstring desContent = (jstring)env->CallObjectMethod(desObj,authcodeId,jbodyContent,jdecodestr,jsceretkey);
    return desContent;
}

/*
 * Class:     com_xqq_lottery_NativeProtocal
 * Method:    DesEncode
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_xqq_lottery_NativeProtocal_DesEncode
  (JNIEnv * env, jclass jcls, jstring jcontent)
{

	jclass descls = env->FindClass("com/xqq/lottery/util/DES");
	jmethodID desMethoId =  env->GetMethodID(descls,"authcode","(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
	jmethodID desInitMethodId = env->GetMethodID(descls,"<init>","()V");
	jstring jencodestr = env->NewStringUTF(ENCODE);
	jstring jsceretkey = env->NewStringUTF(SCERET_KEY);
	jobject desObj = env->NewObject(descls,desInitMethodId);
	jstring jstrECon = (jstring)env->CallObjectMethod(desObj,desMethoId,jcontent,jencodestr,jsceretkey);
	return jstrECon;
}

/*
 * Class:     com_xqq_lottery_NativeProtocal
 * Method:    DesDecode
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_xqq_lottery_NativeProtocal_DesDecode
  (JNIEnv *env, jclass jcls, jstring jconent)
{
	jclass descls = env->FindClass("com/xqq/lottery/util/DES");
	jmethodID desMethoId =  env->GetMethodID(descls,"authcode","(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
	jmethodID desInitMethodId = env->GetMethodID(descls,"<init>","()V");
	jstring jdecodestr = env->NewStringUTF(DECODE);
	jstring jsceretkey = env->NewStringUTF(SCERET_KEY);
	jobject desObj = env->NewObject(descls,desInitMethodId);
	jstring jstrECon = (jstring)env->CallObjectMethod(desObj,desMethoId,jconent,jdecodestr,jsceretkey);
	return jstrECon;
}


//jstring转换成char*
char* jstringTostring(JNIEnv* env,jstring jstr)
{
	char *rtn = NULL;
	jclass strClazz = env->FindClass("java/lang/String");
	jstring strencode = env->NewStringUTF("utf-8");
	jmethodID methodId = env->GetMethodID(strClazz,"getBytes","(Ljava/lang/String;)[B");
	jbyteArray barr =(jbyteArray)env->CallObjectMethod(jstr,methodId,strencode);
	//LOGW("6666666666666666");
	jsize alen = env->GetArrayLength(barr);
	jbyte* ba = env->GetByteArrayElements(barr,JNI_FALSE);
    if(alen > 0)
    {
    	rtn = (char*)malloc(alen+1);
    	memset(rtn,0,alen+1);
    	memcpy(rtn,ba,alen);
    }
    else
    {
    	rtn = (char*)malloc(1);
    	memset(rtn,0,alen+1);
    }
    env->ReleaseByteArrayElements(barr,ba,0);
    return rtn;
}

void releaseStr(char ** rtn)
{
	if(rtn != NULL)
	{
		if(*rtn != NULL)
		{
			free(*rtn);
			*rtn = NULL;
		}
	}

}




